/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.actionManager;

import api.action.ActionTypeEnum;
import api.action.crates.Client.NewUpdateFileCrate;
import tcloudclient.Utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Tomas
 */
public class NewFileActionExecutor implements ExecuteActionInterface<NewUpdateFileCrate> {

    @Override
    public void execute(NewUpdateFileCrate actionData, FireActionExecuteInterface event) {
        if (event.canActionExecute(actionData.getPath(), actionData.getVersion(), tcloudclient.actionManager.ActionTypeClientEnum.NEW)) {
            File file = new File(Utils.generatePaht(actionData.getPath()));
            FileOutputStream fsout = null;
            try {
                fsout = new FileOutputStream(file);
                fsout.write(actionData.getValue());
            } catch (IOException ex) {
                Logger.getLogger(UpdateFileActionExecutor.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    fsout.close();
                    
                } catch (IOException ex) {
                    Logger.getLogger(UpdateFileActionExecutor.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            event.actionExecuted(actionData.getPath(), actionData.getVersion(), tcloudclient.actionManager.ActionTypeClientEnum.NEW);
        }
    }

    @Override
    public boolean isSupported(ActionTypeEnum actionType) {
        return actionType == ActionTypeEnum.NEW_FILE;
    }
}
